package com.cxy.fcms.controller.front;

import com.cxy.fcms.pojo.ComFicDate;
import com.cxy.fcms.pojo.ComFiction;
import com.cxy.fcms.pojo.ComUser;
import com.cxy.fcms.service.CommentService;
import com.cxy.fcms.service.FictionService;
import com.cxy.fcms.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CommentController
 * @Description TODO
 * @Author 码农天宇
 * @Date 2021/5/7 10:32
 * @Version 1.0
 */

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    FictionService fictionService;

    /**
     * @param session 会话
     * @param comment 评论内容
     * @param ficid 书籍id
     * @param model 视图
     * @return String
     * @author 码农天宇
     */
    @RequestMapping("addComment")
    public String addCommentInfo(HttpSession session, String comment, String ficid, String index, Model model){
        System.out.println("current---------------------------" + ficid);
        System.out.println("comment---------------------------" + comment);
        System.out.println("index---------------------------" + index);
        // 获取当前登录用户id
        ComUser user = (ComUser) session.getAttribute("user");
        System.out.println("user---------------------------" + user.getId());
        // 创建map映射关系
        Map<String, Object> commentMap = new HashMap<>();
        commentMap.put("id", IDUtil.getID().trim());
        commentMap.put("userId",user.getId().trim());
        commentMap.put("ficId",ficid.trim());
        commentMap.put("commData",comment.trim());
        commentMap.put("isDeleted",0);
        // 插入评论
        int res = commentService.addComment(commentMap);
        if (res == 1){
            model.addAttribute("succ","评论成功");
            // 加载评论
            model.addAttribute("comment",commentService.getAllComs(ficid.trim()));
        }else{
            model.addAttribute("error","评论失败，系统异常");
        }
        List<ComFiction> fictions = fictionService.getFictionsOrderByHost();
        int index2 = Integer.parseInt(index);
        ComFiction comFiction = fictions.get(index2 - 1);
        //发送数据
        model.addAttribute("ficBook", comFiction);
        //查询内容信息
        ComFicDate data = fictionService.getFictionDataById(comFiction.getId());
        // 记录书籍编号
        model.addAttribute("currentId", comFiction.getId());
        // index
        model.addAttribute("currentIndex", index);
        // 评论数
        model.addAttribute("countComment",commentService.countCommentCurrentBook(comFiction.getId().trim()) + " 评论");
        model.addAttribute("data", data);
        return "front/fiction/info";
    }

    @RequestMapping("orderByFire")
    public String orderByFire(String ficid, String index, Model model){
        // 加载评论
        model.addAttribute("comment",commentService.getAllComsOrderByFire(ficid.trim()));
        List<ComFiction> fictions = fictionService.getFictionsOrderByHost();
        int index2 = Integer.parseInt(index);
        ComFiction comFiction = fictions.get(index2 - 1);
        //发送数据
        model.addAttribute("ficBook", comFiction);
        //查询内容信息
        ComFicDate data = fictionService.getFictionDataById(comFiction.getId());
        // 记录书籍编号
        model.addAttribute("currentId", comFiction.getId());
        // index
        model.addAttribute("currentIndex", index);
        // 评论数
        model.addAttribute("countComment",commentService.countCommentCurrentBook(comFiction.getId().trim()) + " 评论");
        // 数据
        model.addAttribute("data", data);
        return "front/fiction/info";
    }


    @RequestMapping("orderByTime")
    public String orderByTime(String ficid, String index, Model model){
        // 加载评论
        model.addAttribute("comment",commentService.getAllComsOrderByTime(ficid.trim()));
        List<ComFiction> fictions = fictionService.getFictionsOrderByHost();
        int index2 = Integer.parseInt(index);
        ComFiction comFiction = fictions.get(index2 - 1);
        //发送数据
        model.addAttribute("ficBook", comFiction);
        //查询内容信息
        ComFicDate data = fictionService.getFictionDataById(comFiction.getId());
        // 记录书籍编号
        model.addAttribute("currentId", comFiction.getId());
        // index
        model.addAttribute("currentIndex",index);
        // 评论数
        model.addAttribute("countComment",commentService.countCommentCurrentBook(comFiction.getId().trim()) + " 评论");
        // 数据
        model.addAttribute("data", data);
        return "front/fiction/info";
    }

}
